home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / timer.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  2KB  |  98 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "timer.h"
  4.  
  5. /* Head of running timer chain */
  6. struct timer *timers;
  7.  
  8. tick()
  9. {
  10.     register struct timer *t,*tp;
  11.     register struct timer *expired = NULLTIMER;
  12.     char i_state;
  13.     void iostop();
  14.  
  15.     /* Run through the list of running timers, decrementing each one.
  16.      * If one has expired, take it off the running list and put it
  17.      * on a singly linked list of expired timers
  18.      */
  19.     i_state = disable();
  20.     for(t = timers;t != NULLTIMER; t = tp){
  21.         tp = t->next;
  22.         if(tp == t){
  23.             restore(i_state);
  24.             printf("PANIC: Timer loop at %lx\n",(long)tp);
  25.             iostop();
  26.             exit(1);
  27.                 }
  28.         if(t->state == TIMER_RUN && --(t->count) == 0){
  29.             stop_timer(t);
  30.             t->state = TIMER_EXPIRE;
  31.             /* Put on head of expired timer list */
  32.             t->next = expired;
  33.             expired = t;
  34.         }
  35.     }
  36.     restore(i_state);
  37.     /* Now go through the list of expired timers, removing each
  38.      * one and kicking the notify function, if there is one
  39.      */
  40.     /* Note: the check for TIMER_EXPIRE below has a specific */
  41.     /* purpose.  It prevents wasted timer calls to the NET/ROM */
  42.     /* transport protocol timeout routine.  This routine does */
  43.     /* not know which timer expired, so it scans and processes */
  44.     /* the whole window.  If multiple timers expired, it handles */
  45.     /* them all and resets their states to something other than */
  46.     /* TIMER_EXPIRE.  So, we oblige here by not re-processing */
  47.     /* them under those circumstances. */
  48.     
  49.     while((t = expired) != NULLTIMER){
  50.         expired = t->next;
  51.         if(t->state == TIMER_EXPIRE && t->func){
  52.             (*t->func)(t->arg);
  53.         }
  54.     }
  55. }
  56. /* Start a timer */
  57. start_timer(t)
  58. register struct timer *t;
  59. {
  60.     char i_state;
  61.  
  62.     if(t == NULLTIMER || t->start == 0)
  63.         return;
  64.     i_state = disable();
  65.     t->count = t->start;
  66.     if(t->state != TIMER_RUN){
  67.         t->state = TIMER_RUN;
  68.         /* Put on head of active timer list */
  69.         t->prev = NULLTIMER;
  70.         t->next = timers;
  71.         if(t->next != NULLTIMER)
  72.             t->next->prev = t;
  73.         timers = t;
  74.     }
  75.     restore(i_state);
  76. }
  77. /* Stop a timer */
  78. stop_timer(t)
  79. register struct timer *t;
  80. {
  81.     char i_state;
  82.  
  83.     if(t == NULLTIMER)
  84.         return;
  85.     i_state = disable();
  86.     if(t->state == TIMER_RUN){
  87.         /* Delete from active timer list */
  88.         if(timers == t)
  89.             timers = t->next;
  90.         if(t->next != NULLTIMER)
  91.             t->next->prev = t->prev;
  92.         if(t->prev != NULLTIMER)
  93.             t->prev->next = t->next;
  94.     }
  95.     t->state = TIMER_STOP;
  96.     restore(i_state);
  97. }
  98.